package com.wyyx.dao;

import java.sql.SQLXML;
import java.util.HashMap;
import java.util.List;

import com.wyyx.model.Coupon;

import utils.SqlHelper;
import utils.setAttrUtil;

public class CouponDaoImpl {
	/**
	 * 后台添加优惠券
	 * @param coupon
	 * @return
	 */
	public int  insertCoupon(Coupon coupon) {
		String sql = "insert into coupon(coupon_id,coupon_name,coupon_discount_price,coupon_limit_price,"
				+ "coupon_start_time,coupon_end_time) values(uuid(),?,?,?,?,?)";
		int i = SqlHelper.update(sql,coupon.getCouponName(),coupon.getCouponDiscountPrice(),coupon.getCouponLimitPrice(),coupon.getCouponStartTime(),coupon.getCouponeEndTime());
		SqlHelper.close();
		return i;
	}
	
	/**
	 * 后台删除优惠券
	 * @param couponId
	 * @return
	 */
	public int deleteCoupon(String couponId) {
		String sql = "update coupon set coupon_status = 0 where coupon_id = ?";
		int i = SqlHelper.update(sql,couponId);
		SqlHelper.close();
		return i;
	}
	
	/**
	 * 领取优惠券
	 */
	
	public int takeCoupon(String userId,String couponId) {
		String sql="update coupon set coupon_user_id = ? where coupon_id = ?";
		int i = SqlHelper.update(sql,userId,couponId);
		SqlHelper.close();
		return i;
	}
	
	/**
	 * 查询我的优惠券
	 * 
	 */
	public List<Coupon> selectCoupon(String userId){
		String sql = "select * from coupon where coupon_user_id = ?";
		List<HashMap<String,Object>> list = SqlHelper.select1(sql,userId);
		List<Coupon> couponList = setAttrUtil.setCoupon(list);
		SqlHelper.close();
		return couponList;
	}
	
	
	
}
